﻿@using ldc_mvc_closing_hwk.Models.Database
@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@


@{
    if (ViewBag.ArticleList.Count == 0)
    {
        <h2>这里没有内容</h2>
    }
}

<ul id="main_ul">
    @{
        // 渲染文章内容
        var articleList = ViewBag.ArticleList;
        foreach (ArticleMetum item in articleList)
        {
            var coverImg = item.CoverImage ?? "~/img/default.png";

            <li>
                <a href="detail_@(item.Id).html">
                    <img src="@Url.Content(coverImg)">
                    <p>@item.Title</p>
                    <p style="word-break: break-all">
                        @item.Summary
                    </p>
                    <p>发布时间: @item.ReleaseTime.ToString() </p>
                    <p>文章来源: @item.SourceName </p>
                </a>
            </li>
        }
    }
</ul>

@section PageNav {
    <div class="paging">
        <ul class="pagination">
            @{
                // 上一页
                var lastPage = "javascript:void(0)";
                if (ViewBag.CurrentPage > 1)
                {
                    lastPage = $"javascript:changePage({(ViewBag.CurrentPage - 1)})";
                }
                <li><a href="@lastPage">«</a></li>
            }

            @{
                int maxPage = ViewBag.PageCount;
                int curPage = ViewBag.CurrentPage;

                int vector = 2; // 两端各展示多少个
                int showStart = Math.Max(1, curPage - vector);
                int showEnd = Math.Min(maxPage, curPage + vector);

                int leftFree = showStart-1;
                int rightFree = maxPage - showEnd;

                // 令余剩的内容小于等于二时可以直接显示全部
                if (leftFree <= 2)
                {
                    showStart = 1;
                }
                if (rightFree <= 2)
                {
                    showEnd = maxPage;
                }

                if (leftFree > 2)
                {
                    var vectorNextPage = Math.Max(1, curPage - vector * 2 -1);

                    <li><a href="javascript:changePage(1)">1</a></li>
                    <li><a href="javascript:changePage(@vectorNextPage)">...</a></li>
                }

                // 选择页
                for (int i = showStart; i <= showEnd; i++)
                {
                    var pageLink = $"javascript:changePage({i})";
                    var activeFlag = "";

                    if (i == curPage)
                    {
                        pageLink = "javascript:void(0)";
                        activeFlag = "active";
                    }

                    <li><a class="@activeFlag" href="@pageLink">@i</a></li>
                }

                if (rightFree > 2)
                {
                    var vectorNextPage = Math.Min(maxPage, curPage + vector * 2 +1);

                    <li><a href="javascript:changePage(@vectorNextPage)">...</a></li>
                    <li><a href="@($"javascript:changePage({maxPage})")">@maxPage</a></li>
                }


            }

            @{
                // 下一页
                var nextPage = "javascript:void(0)";
                if (ViewBag.PageCount > ViewBag.CurrentPage)
                {
                    nextPage = $"javascript:changePage({(ViewBag.CurrentPage + 1)})";
                }
                <li><a href="@nextPage">»</a></li>
            }
        </ul>
    </div>

    <script>
        function changePage(page) {
            window.location.search = setQueryString("page", page)
        }
    </script>

}